package com.lc.stock.module.filtrate.filter.impl;

import cn.hutool.core.collection.CollUtil;
import com.lc.stock.module.filtrate.filter.FiltrateStockFilter;
import com.lc.stock.module.stockholdernum.dao.StockholderNumDao;
import com.lc.stock.module.stockholdernum.domain.StockholderNum;
import com.lc.stock.module.stockholdernum.domain.StockholderNumDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lichen
 * @description 筛选出 (近n个季度/时间周期半年以上)股东户数持续减少
 *          由于部分公司 公布的股东户数不止公布季度日期的所以此条件更换满足以下2个条件即可：
 *          1. 近m个季度股东户数，持续减少
 *          2. 近n个公告日期，股东户数持续减少
 * @menu
 * @date 2023/2/6 14:10
 */
@Slf4j
@Component
public class StockHolderNumDecrease implements FiltrateStockFilter {

    /**
     * 股东持续减少（单位：截止日期）
     */
    @Value("${coustomfilter.stockHolderDecreaseTime}")
    private int stockHolderDecreaseTime;
    /**
     * 股东持续减少（单位：季度日期）
     */
    @Value("${coustomfilter.stockHolderDecreaseJdTime}")
    private int stockHolderDecreaseJdTime;

    @Autowired
    private StockholderNumDao stockholderNumDao;

    @Override
    public boolean doFilter(String stockCode) {
        StockholderNum byStockCode = stockholderNumDao.findByStockCode(stockCode);
        if(byStockCode == null){
            return true;
        }
        //size>=2 表示最少有3个季度的股东户数
        List<StockholderNumDetails> stockholderNumList = byStockCode.getStockholderNumList();
        if(CollUtil.isNotEmpty(stockholderNumList) && stockholderNumList.size() >= (stockHolderDecreaseTime-1)){
            /**
             * 判断是否符合条件：2.近n个公告日期，股东户数持续减少
             */
            //判断 list 中的股东户数，是否持续减少
            List<StockholderNumDetails> list = stockholderNumList.subList(0, stockHolderDecreaseTime);
            boolean b1 = true;
            for (int i = 0; i < list.size()-1; i++) {
                StockholderNumDetails details_i1 = list.get(i);
                StockholderNumDetails details_i2 = list.get(i + 1);

                if(details_i2.getGdhs() < details_i1.getGdhs()){
                    //股东户数不是持续减少
                    b1 = false;
                    break;
                }
            }

            /**
             * 判断是否符合条件：1. 近2个季度股东户数，持续减少
             */
            boolean b2 = true;
            //从stockholderNumList中获取最近m个季度的数据
            List<String> jdDatelist = new ArrayList<>();
            jdDatelist.add("03-31");
            jdDatelist.add("06-30");
            jdDatelist.add("09-30");
            jdDatelist.add("12-31");
            ArrayList<Integer> jdGdhsList = new ArrayList<>();
            for (StockholderNumDetails stockholderNumDetails : stockholderNumList) {
                if(jdGdhsList.size() >= stockHolderDecreaseJdTime){
                    break;
                }
                String jdrq = stockholderNumDetails.getJzrq().substring(5);
                if(jdDatelist.contains(jdrq)){
                    jdGdhsList.add(stockholderNumDetails.getGdhs());
                }
            }
            //判断最近m个季度的股东户数是否持续减少
            for (int i = 0; i < jdGdhsList.size()-1; i++) {
                int var1 = jdGdhsList.get(i);
                int var2 = jdGdhsList.get(i + 1);
                if(var2 < var1){
                    //股东户数不是持续减少
                    b2 = false;
                    break;
                }
            }

            /**
             * 满足任意一个条件 则返回true
             */
            if(b1 || b2){
                return true;
            }else{
                return false;
            }
        }
        return true;
    }

    @Override
    public String getErrorMsg() {
        return "股东近" + stockHolderDecreaseTime + "季度股东户数非持续减少";
    }
}
